package com.zhangtai.modules.service.impl.employee;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.RelativeRecordsDao;
import com.zhangtai.modules.entity.employee.RelativeRecordsEntity;
import com.zhangtai.modules.service.employee.RelativeRecordsService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class RelativeRecordsServiceImpl extends ServiceImpl<RelativeRecordsDao, RelativeRecordsEntity> implements RelativeRecordsService {

    @Override
    public PageResult queryPage(@RequestParam(value = "oid", required = false) String oid,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<RelativeRecordsEntity> page = this.page(
                new Page<RelativeRecordsEntity>(pageNum,pageSize),
                new QueryWrapper<RelativeRecordsEntity>().eq(!StringUtils.isEmpty(oid),"oid",oid)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public RelativeRecordsEntity getByIdE(Long id) {
            RelativeRecordsEntity one = this.getOne(new QueryWrapper<RelativeRecordsEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public List<RelativeRecordsEntity> getByOid(Long oid){
        List<RelativeRecordsEntity> list = this.list(new QueryWrapper<RelativeRecordsEntity>().eq("oid",oid).eq("is_delete","0"));
        return list;
    }

    @Override
    public R saveE(RelativeRecordsEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(RelativeRecordsEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            RelativeRecordsEntity entity = new RelativeRecordsEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}